<template>
  <div>
    <el-row>
      <el-col :span="3">
        <div>

        </div>
        <el-menu
          router
          style="height: 100vh"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b">
          <el-menu-item style="height: 150px;text-align: center">
            <el-upload
              class="upload-demo"
              :on-change="handleChange"
              :on-success="handleSuccess"
              :show-file-list="false"
              action="http://localhost:8080/upload/imgUpload">
              <img v-if="avatarPath" :src="avatarPath" width="80px"/>
              <el-avatar v-if="!avatarPath" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
            </el-upload>
            <h1 style="color: white">{{user.name}}({{user.role == 1 ? '商家' : '用户'}})</h1>
          </el-menu-item>
          <el-submenu index="1" v-if="user.role == 1">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>菜品管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/addFood">添加菜品</el-menu-item>
              <el-menu-item index="/foodList">菜品列表</el-menu-item>
            </el-menu-item-group>

          </el-submenu>
          <el-menu-item index="orderFood" v-if="user.role == 0">
            <i class="el-icon-menu"></i>
            <span slot="title">在线点餐</span>
          </el-menu-item>
          <el-menu-item index="userList" v-if="user.role == 1">
            <i class="el-icon-document"></i>
            <span slot="title">用户信息</span>
          </el-menu-item>
          <el-menu-item index="orderInfo">
            <i class="el-icon-document"></i>
            <span slot="title">订单信息</span>
          </el-menu-item>
          <el-submenu index="3" v-if="user.role == 1">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>公告管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/addNotice">添加公告</el-menu-item>
              <el-menu-item index="/noticeList">公告列表</el-menu-item>
            </el-menu-item-group>

          </el-submenu>
          <el-menu-item @click="logout">
            <i class="el-icon-switch-button"></i>
            <span slot="title" >退出</span>
          </el-menu-item>

        </el-menu>
      </el-col>
      <el-col :span="21">
        <router-view/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "Home",
  data(){
    return{
      user: JSON.parse(localStorage.getItem("user")),
      avatarPath: ""
    }
  },
  mounted() {
    this.getAvatar()
  },
  methods:{
    logout(){
      localStorage.clear()
      this.$router.push('/')
    },
    getAvatar(){
      console.info(this.user.pic)
      if (this.user.pic != null) {
        this.avatarPath = "http://localhost:8080/pic/" + this.user.pic
      }else {
        this.avatarPath = ""
      }

    },
    handleChange(file, fileList){
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }
      console.info(file)
      this.avatarPath = URL.createObjectURL(file.raw);
      console.info(this.avatarPath)
    },
    handleSuccess(response){
      this.$message.success("上传文件成功！")
      console.info(response.data)
      let path = response.data
      this.avatarPath = path
      this.updateAvatar(path)
      // console.info("pic",JSON.parse(localStorage.getItem("user")).pic)
      const user = JSON.parse(localStorage.getItem("user"))
      user.pic = path
      // console.info("user", user)
      localStorage.setItem("user", JSON.stringify(user))
    },
    updateAvatar(path){
      this.axios.post("http://localhost:8080/user/updateAvatar?pic="+path+"&id="+this.user.id).then(res => {
        console.info(res.data)
      })
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
